package runableEx;

import java.util.ArrayList;

/**
 * @author ZhuQi
 * @ClassName Demo2
 * @Date 2022/10/18 13:54
 * @Version 1.0
 * @Description TODO
 */
public class Demo2 {

    public static void main(String[] args) {
        // runnableParamRuler();
        String[] names = {"zs","ls","ww"};
        ArrayList<Runnable> runnables = new ArrayList<>();
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            runnables.add(()-> System.out.println(name));

            // 不合法，i 不是常量不满足闭包条件
            // runnables.add(()-> System.out.println(names[i]));
        }
    }

    private static void runnableParamRuler() {
        String[] names = {"zs","ls","ww"};
        ArrayList<Runnable> runnables = new ArrayList<>();
        for (String name : names) {
            runnables.add(()->{
                System.out.println(name);
            });
        }

        for (Runnable runnable : runnables) {
            runnable.run();
        }
    }
}
